var database = require('../../data/data.js')
var app = getApp();
Page({
  data: {
    tabbar: {},
    teamCount: 0,
    // money: 0,
    msg: 0,
  },
  onLoad: function () {
    var that = this;
    wx.request({
      url: database.url1 + '/api/V2/user/verify_wechat_small',
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        unionid: database.unionid,
        accessToken: database.accessToken
      },
      success(res) {
        if (res.data.data.verify == "no") {
          database.login = false;
        } else {
          database.login = true;
        }
      }
    })
    this.setData({
      nickName: database.nickName,
      avatarUrl: database.avatarUrl
    })
    app.editTabBar();
  },
  onShow() {
    var that = this;
    if (database.login != false) {
      // wx.request({
      //   url: database.url1 + '/api/V2/user/currency',
      //   data: {
      //     accessToken: database.accessToken,
      //     userId: database.userId
      //   },
      //   method: "POST",
      //   header: {
      //     'content-type': 'application/x-www-form-urlencoded'
      //   },
      //   success(res) {
      //     that.setData({
      //       money: res.data.data.currency
      //     })
      //   }
      // });
      wx.request({
        url: database.url1 + '/api/V2/softball/softball_list',
        data: {
          accessToken: database.accessToken,
          userId: database.userId
        },
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success(res) {
          var teamData = res.data;
          if (teamData.data.softballList != null) {
            that.setData({
              teamCount: teamData.data.softballList.length
            })
          } else {
            that.setData({
              teamCount: 0
            })
          }
        }
      });
      wx.request({
        url: database.url1 + '/api/V2/softball/softball_list',
        data: {
          accessToken: database.accessToken,
          userId: database.userId
        },
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success(res) {
          var teamData = res.data;
          if (teamData.data.softballList != null) {
            that.setData({
              teamCount: teamData.data.softballList.length
            })
          } else {
            that.setData({
              teamCount: 0
            })
          }
        }
      });
      wx.request({
        url: database.url + '/project/getMyMessage.action',
        data: {
          openid: database.userId
        },
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success(res) {
          var yuezhanamessage = res.data.yuezhanamessage;
          var cancelamessage = res.data.cancelamessage;
          var nmessage = res.data.nmessage;
          // var rewardmessage = res.data.rewardmessage;
          that.setData({
            msg: yuezhanamessage.length + cancelamessage.length + nmessage.length 
            // + rewardmessage.length
          });
        }
      });
    }
  },
  add: function (e) {
    wx.navigateTo({
      url: '/pages/fight/fight',
    })
  },
  about: function (e) {
    wx.navigateTo({
      url: '/pages/my/about/about',
    })
  },
  money: function (e) {
    wx.navigateTo({
      url: '/pages/my/recharge/recharge',
    })
  },
  setting: function (e) {
    wx.navigateTo({
      url: '/pages/my/setting/setting',
    })
  },
  toFight: function () {
    wx.navigateTo({
      url: '/pages/my/my_add_fight/my_add_fight',
    })
  },
  msg: function (e) {
    wx.navigateTo({
      url: '/pages/my/message/message',
    })
  },
  myteam: function () {
    wx.navigateTo({
      url: '/pages/my/myteam/myteam/myteam',
    })
  },
  h5:function(){
    wx.navigateTo({
      url: '/pages/web_test/web_test',
    })
  }
})